'use strict';

/*var app = angular.module('app');*/
angular.module('app').controller('maintenanceUpdateController', [ '$rootScope', '$scope', '$http', '$state','toaster','$modal','$timeout',
    function($rootScope, $scope, $http, $state,toaster,$modal,$timeout) {
        $scope.title = '维修人员新增';
        $scope.param = { };
        $scope.search = function () {
            $scope.loading = true;
            
            if($scope.pageFlag != 1){//未点击翻页
                $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
            }else{//点击翻页
                $scope.pageFlag = 2;//将开关值设置为2
            }
            $.ajax({
                type: 'PUT',
                dataType: 'json',
                contentType:'application/json;charset=UTF-8',
                url : 'dac/userManager/listAppUser',
                data: angular.toJson($scope.param)
            }).then(function(result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                    $scope.pageInfo = result;
                } else {
                    $scope.msg = result.msg;
                }
                $scope.$apply(function(){
                    $("#allCheck").prop("checked",false); //去掉全选按钮
                });
            });
        }
        $scope.search();

        //从本地获取localstorage数据，对权限按钮做隐藏
        var personAuth =localStorage.getItem("PersonAuth");//获取PersonAuth的值
        personAuth = personAuth.split(',');
        for(var i = 0;i<personAuth.length;i++){
            if(personAuth[i]=='main.member.read'){
                $scope.read = true;
            }
            if(personAuth[i]=='main.member.add'){
                $scope.add = true;
            }
            if(personAuth[i]=='main.member.update'){
                $scope.update = true;
            }
            if(personAuth[i]=='main.member.delete'){
                $scope.dele = true;
            }
        }

        //单个添加
        $scope.chooceItem = function(id,personName) {
        	
            if (confirm("确定添加【"+ personName+"】为维修人员？") == true )
            {
            	var user_id = [id];
                $scope.params = { };
                $scope.params.user_id = user_id;
                $scope.params.groupCode = $state.params.groupCode;
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    contentType:'application/json;charset=UTF-8',
                    url : 'dac/userManager/personMaintain/saveMaintain',
                    data: angular.toJson($scope.params)
                }).then(function(result) {
                    if (result.httpCode == 200) {
                        toaster.clear('*');
                        toaster.pop('success', '', "添加成功");
                        $timeout(function(){
                            $state.go('main.member.maintenance.group',null,{
                                reload:true
                            });
                        },1000);
                    }
                    else
                    {
                        toaster.clear('*');
                        toaster.pop('false', '', result.msg);
                    }
                    $scope.$apply();
                });
            }
            else
            {
                toaster.clear('*');
                toaster.pop('false', '','已取消');
            };



        };

        //全选按钮
        $scope.allCheck = function($event){
            var allcheck = $event.target ;//全选按钮
            var checked = allcheck.checked ;//选中状态
            var checkBoxs = $(".tbody input[name='Fruit']");
            checkBoxs.each(function(i){
                this.checked = checked;
            });
        }
        //检测没有全选就将全选按钮变为未选状态
        $scope.checkStatus = function () {
            var checkBoxs = $(".tbody input[name='Fruit']").length;//单选框的个数
            var checkeds = $(".tbody input[name='Fruit']:checked").length//单选框选中的个数
            $("#allCheck").prop("checked",checkBoxs === checkeds ? true : false);//个数与选中个数相同时，全选按钮选中，否则不选中
        }
        //批量添加
        $scope.allChooce = function () {
            var checkBoxs = $('.tbody').find('input[type=checkbox]');
            var idArray = [];//存放id数组
            checkBoxs.each(function (i) {
                if (this.checked == true) {
                    idArray.push(this.value);//遍历表格的单选框，如果选中，就获取他value里存的id
                }
            });
            if (idArray.length <= 0) {//如果未选中任何人员，此处批量添加人员将不能执行
                toaster.clear('*');
                toaster.pop('error', '', '至少选择一个人员！');
                return
            } else if (idArray.length > 0) {//已经有选中的人员
                $scope.params = { };
                $scope.params.user_id = idArray;
                $scope.params.groupCode = $state.params.groupCode;
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    contentType:'application/json;charset=UTF-8',
                    url : 'dac/userManager/personMaintain/saveMaintain',
                    data: angular.toJson($scope.params)
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        toaster.clear('*');
                        toaster.pop('success', '','添加成功！');
                        $timeout(function(){
                            $state.go('main.member.maintenance.group',null,{
                                reload:true
                            });
                        },1000);
                    } else {
                        console.log($scope.msg = result.msg);
                        toaster.clear('*');
                        toaster.pop('error', '', result.msg);
                    }
                    $scope.$apply();
                });

            }
        }
        // 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.pageFlag = 1;//将开关值设置为1
            $scope.search();
        };
    } ]);



